package com.reader.api.service.impl.usercenter;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.reader.api.entity.usercenter.APPToken;
import com.reader.api.entity.usercenter.DeviceInfo;
import com.reader.api.entity.usercenter.ReaderAppUserLog;
import com.reader.api.entity.usercenter.User;
import com.reader.api.mapper.usercenter.UserMapper;
import com.reader.api.service.usercenter.IAPPTokenService;
import com.reader.api.service.usercenter.IReaderAppUserLogService;
import com.reader.api.service.usercenter.IUserService;
import com.reader.core.redis.RedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 用户实现类
 * </p>
 *
 * @author jiahaikun
 * @date 2018-07-26
 */
@Service("iSysUserService")
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
  @Autowired
  private UserMapper userMapper;
  @Autowired
  private RedisDao redisDao;

  @Autowired
  private IAPPTokenService iTokenService;

  @Autowired
  private IReaderAppUserLogService iReaderAppUserLogService;


    @Override
    public User getUserInfo(User user) {
        User userInfo = userMapper.getUserInfo(user);
        return userInfo;
    }

    @Override
    public User getUserInfo(String mobile) {
         User user=new User();
         user.setMobile(mobile);
         User userInfo = userMapper.getUserInfo(user);
        return userInfo;
    }

    @Override
    public Page<Map<String, Object>> selectListPage(int currentPage, int size, String loginId, String name, String code) {
        Page<Map<String, Object>> page = new Page<>(currentPage, size);// 当前页，总条数 构造 page 对象
        return page.setRecords(userMapper.selectListPage(page, loginId, name,code));
    }

    @Override
    public Integer getUserIdByToken(String token) {
        APPToken tokenInfo=iTokenService.getTokenInfoByToken(token);
        if(tokenInfo==null||tokenInfo.getUserID()==null){
            return null; //查不到userID
        }else {
            return tokenInfo.getUserID();
        }
    }

    @Override
    public User getUserByMobile(String mobile) {
        User user=new User();
        user.setMobile(mobile);
        return userMapper.selectOne(user);
    }

    @Transactional
    @Override
    public APPToken login(String mobile, DeviceInfo deviceInfo) {
        APPToken token=null;
        User user=getUserInfo(mobile);

        if(user==null||user.getId()==null){
            user=new User();
            user.setMobile(mobile);
            this.insert(user);   //未注册用户，则自动注册
        }

        ReaderAppUserLog readerAppUserLog=new ReaderAppUserLog();
        readerAppUserLog.setDeviceId(deviceInfo.getDeviceId());
        readerAppUserLog.setAppUserId(user.getId());
        readerAppUserLog.setDeviceModelType(deviceInfo.getDeviceModelType());
        readerAppUserLog.setToken(deviceInfo.getToken());
        readerAppUserLog.setLastLoginDt(new Date());
        iReaderAppUserLogService.insert(readerAppUserLog);//保存访问日志

        token= iTokenService.createToken(user.getId());//创建返回token
        token.setUserID(null);//返回前端去掉userid
        return token;
    }


}
